// utility.js

// The same as chrome.windows.getLastFocused, but compatible with all 
// currently supported Chrome versions.
function GetLastFocusedWindow(callback)
{
	try
	{
		chrome.windows.getLastFocused(null, callback);
	}
	catch(e)
	{
		chrome.windows.getLastFocused(callback);
	}
}

// Create a tab, eg. to show a new message in a non-incognito window
function CreateTab(relative_url)
{
	GetLastFocusedWindow(function(win)
	{
		if(!win.incognito && win.type == "normal")
			chrome.tabs.create({windowId: win.id, 
				url: XHR.GetURL(relative_url)});
		else
			chrome.windows.getAll(function(wins)
			{
				//for(var i = 0; i < wins.length; ++i)
				wins.forEach(function(cur)
				{
					if(!cur.incognito && cur.type == "normal")
					{
						chrome.tabs.create({windowId: cur.id, 
							url: XHR.GetURL(relative_url)});
						chrome.windows.update(cur.id, {focused: true});
						return;
					}
				});
				//All windows found are incognito. Create a non-incognito
				//one, along with the new tab
				chrome.windows.create({url: XHR.GetURL(relative_url), 
					incognito: false, focused: true});
			});
	});
}

function GetAllBBSTabs(callback)
{
	function filter(tab)
	{
		return /^http:\/\/bbs\.(stuhome\.net|uestc6\.edu\.cn|qshpan\.com|germanyt\.com)\//.test(tab.url);
	}
	if(chrome.tabs.query)
		chrome.tabs.query({}, function(tabs){
			callback(tabs.filter(filter));
		});
	else
		chrome.windows.getAll({populate: true}, function(windows)
		{
			var tabs = [];
			windows.forEach(function(cur)
			{
				tabs = tabs.concat(cur.tabs.filter(filter));
			});
			callback(tabs);
		});
}

function GetURLFromCookie(cookie)
{
	return (cookie.secure ? "https" : "http") + "://" + cookie.domain + 
		cookie.path;
}

function UpdateCookie(updates, oldCookie, callback)
{
	var newCookie = 
	{
		url: GetURLFromCookie(oldCookie), 
		name: oldCookie.name, 
		value: updates.value || oldCookie.value, 
		path: oldCookie.path, 
		secure: updates.secure != undefined ? 
			updates.secure : (oldCookie.secure || false), 
		httpOnly: updates.httpOnly != undefined ? 
			updates.httpOnly : (oldCookie.httpOnly || false ), 
	};
	var expire = updates.expirationDate || oldCookie.expirationDate;
	var storeId = updates.storeId || oldCookie.storeId;
	if(expire)
		newCookie.expirationDate = expire;
	if(!oldCookie.hostOnly && oldCookie.domain)
		newCookie.domain = oldCookie.domain;
	if(storeId)
		newCookie.storeId = storeId;
	chrome.cookies.set(newCookie, callback);
}

var Notifications = [];
function ShowNotification(source, details, callback)
{
	var title;
	var content;
	if(source == "message")
	{
		title = "您有新消息了！";
		content = details.fromUser + "：" + details.title;
	}
	else if(source == "subscription")
	{
		if(details.type == "RepliesToPosts")
		{
			title = "您订阅的帖子有新回复了！";
			content = details.title + ( details.user ? 
					("：由" + details.user + "回复") : "");
		}
		else
			return;
	}
	else
		return;
	var notification = webkitNotifications.createNotification(
		"images/icons/qshpan_48.png", title, content);
	Notifications.push(notification);
	notification.onclick = (function()
	{
		callback && callback();
		this.cancel();
	});
	notification.show();
}

